<?php

class WeibosController extends AppController{
	public $Helpers = array ('Html' , 'Form' );
	public $uses = array('Weibo', 'Comment', 'Thread', 'Message', 'User');

	public function index() {
		$userid = $this->Session->read('userid');
		if ( $userid ) {
			$this->set( 'posts' , $this->Weibo->find(
				'all',
				array ('joins' => array( array( 
					'alias' => 'Follow',
					'table' => 'follows',
					'conditions' => 'Follow.to = Weibo.user_id and Follow.from ='.$userid,
					'type' => 'INNER',
				)),
				'order' => 'date DESC'
			)));
		} else {
			$this->weibosByUser();
		}
	}

	public function weiboView($id = null) {
		if (!$id) {
			throw new NotFoundException (__('Invaild post'));
		}

		$post = $this->Weibo->findById($id);

		$this->set( 'comments' , $this->Comment->find(
			'all',
			array('conditions' => array('weibo_id' => $id),'order' => 'date DESC')
		));

		if(!$id) {
			throw new NotFoundException(__('Invaild post'));
		}

		$this->set('post' , $post);
	}

	public function weibosByUser($id = null) {
		if (!$id){
			$this->set( 'posts' , $this->Weibo->find(
				'all',
				array('order' => 'date DESC',)
			));
		} else {
			$this->set( 'posts' , $this->Weibo->find(
				'all',
				array('order' => 'date DESC',
				'conditions' => array('user_id' => $id )
			)
			));
		}
	}

	public function weiboAdd() {
		if ( $this->request->is('post')) {
			$this->Weibo->create();
			$this->request->data['Weibo']['user_id'] = $this->Session->read('userid');
			$this->request->data['Weibo']['date'] = date('Y-m-d H:i:s');
			if ($this->Weibo->save($this->request->data)){
				$this->Session->setFlash(__('Your weibo has been saved.'));
				return $this->redirect(array('action' => 'index'));
			}

			$this->Session->setFlash(__('Unable to add your weibo.'));
		}
	}

	public function weiboEdit($id = null) {

		if(!$id) {
			throw new NotFoundException(__('Invalid post'));
		}

		$post = $this->Weibo->findById($id);
		if(!$post) {
			throw new NotFoundException(__('Invalid post'));
		}

		if($this->request->is(array('post' , 'put'))) {
			$this->Weibo->id = $id;
			$this->request->data['Weibo']['date'] = date('Y-m-d H:i:s');
			if ($this->Weibo->save($this->request->data)) {
				$this->Session->setFlash(__('Your Weibo has benn updated.'));
				return $this->redirect(array('action' => 'index'));
			}
			$this->Session->setFlash(__('Unable to update your Weibo.'));
		}
		if(!$this->request->data) {
			$this->request->data = $post;
		}
	}

	public function weiboDelete($id) {
		if ($this->request->is('get')) {
			throw new MethodNotAllowedException();
		}

		if ($this->Weibo->delete($id)) {
			$this->Session->setFlash(__('The Weibo has been deleted.'));
			return $this->redirect(array('action' => 'index'));
		}
	}

	public function weiboSearch($string) {
		if (!$id) {
			return $this->redirect(array('action' => 'index'));
		}
		$searchs = $this->Weibo->find();
	}

	public function commentAdd($wid = null) {
		$this->set( 'wid' , $wid );
		if( $this->request->is('post')) {
			$this->Comment->create();
			$this->request->data['Comment']['weibo_id'] = $wid;
			$this->request->data['Comment']['user_id'] = $this->Session->read('userid');
			$this->request->data['Comment']['date'] = date('Y-m-d H:i:s');
			if ($this->Comment->save($this->request->data)) {
				$this->Session->setFlash(__('Your comment has been saved.'));
				return $this->redirect(array(
					'controller' => 'weibos',
					'action' => 'weiboView',
					$wid
				));
			$this->Session->setFlash(__('Unable to add a comment.'));
			}
		}
	}

	public function commentDelete ($id = null , $wid = null) {
		if ($this->request->is('get')) {
			throw new MethodNotAllowedException();
		}

		if ($this->Comment->delete($id)) {
			$this->Session->setFlash(__('The Comment has been deleted.'));
			return $this->redirect(array(
				'controller' => 'weibos',
				'action' => 'weiboView',
				$wid
			));
		}
	}

}

?>

